<?php

abstract class Application_Model_AbstractMapper {

	protected $_dbTableName = ""; // assigned by particular mappers propagated to Zend_Db_Table_Abstract
	private $_dbTableModel;

	protected function _invalidateCache($key) {
		//* @todo ev. diese Funktionalität in Registry, damit *global* verfügbar
		$auth = Zend_Auth::getInstance();
		$key = "";
		if ($auth->hasIdentity()) {
			$userId = $auth->getIdentity();
			$key = $userId[1];
		}

		$r = Zend_Registry::getInstance();
		$r->pogCache->remove("$key");
		// $r->pogCache->clean(Zend_Cache::CLEANING_MODE_ALL);
	}

	/**
	 * @param string $dbTableModel
	 * @return Application_Model_AbstractMapper
	 * @throws Exception
	 */
	public function setDbTable($dbTableModel) {
		/**
		 * @var Zend_Db_Table_Abstract $_dbTableModel
		 */
		if (is_string($dbTableModel)) {
			$dbTableModel = new $dbTableModel($this->_dbTableName);
		}
		if (!$dbTableModel instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTableModel = $dbTableModel;
		return $this;
	}

	/**
	 * @return Zend_Db_Table_Abstract
	 */
	public function getDbTable() {
		if (null === $this->_dbTableModel) {
			$this->setDbTable('Application_Model_DbTable_DB');
		}
		return $this->_dbTableModel;
	}


	/**
	 * @param  Zend_Db_Table_Row_Abstract $row
	 * @return Application_Model_Blog
	 */
	abstract protected function _convertToModel($row);

	/**
	 * @param Zend_DB_Table_Rowset_Abstract $resultSet
	 * @return Application_Model_Abstract[]
	 */
	protected function _convertToModelArray($resultSet) {
		$arr = array();
		foreach ($resultSet as $row) {
			$arr[] = $this->_convertToModel($row);
		}
		return $arr;
	}
}

